/**
 * 拜访记录 - 编辑页面
 * @author miaoyl
 * @date 2019/04/12
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            isAdd: false,
            disabled:false,
            form: {
                GWAKL004: '',
                GWBGL003: '',
                GWAKL003: '',
                GWAVL003: ''
            },
            gwbf:[],
            activeName2: 'first',
            rules: {
                GWAKL004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBGL003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBH003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBH001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAVL003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBH034: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBH030: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBH031: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWBH010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                crm_gwat160_gwbh001:app.datas_("crm_gwat160_gwbh001"),
                crm_gwat160_gwbh034:app.datas_("crm_gwat160_gwbh034"),
                crm_gwat160_gwbh031:app.datas_("crm_gwat160_gwbh031")
            },
        },
        table_where = {

        },
        cols1 = [[
            {field: 'GWBKSEQ',type: 'checkbox', title: '选择'}
            , {field: 'GWBK001', width: 80, title: '商品编号'}
            , {field: 'GWAML003', width: 240, title: '品名'}
            , {field: 'GWAML004', width: 80, title: '规格'}
            , {field: 'GWBK003', width: 80, title: '数量', edit: 'text'}
            , {field: 'GWBK004', width: 80, title: '单价', edit: 'text'}
            , {field: 'GWBK005', width: 80, title: '金额', edit: 'text'}
            , {field: 'GWBK006', width: 80, title: '折扣率', edit: 'text'}
            , {field: 'GWBK007', width: 140, title: '折扣后金额', edit: 'text'}
            , {field: 'GWBK008', width: 140, title: '预计购买日期', edit: 'date'}
            , {field: 'GWBK009', width: 140, title: '预计回款日期', edit: 'date'}
            , {field: 'GWBK002', width: 80, title: '单位', hide:true}
        ]],
        cols2 = [[
            {field: 'STATUS', type: 'checkbox', title: '选择'}
            , {field: 'GWBC001', width: 160, title: '关键评估编号'}
            , {field: 'GWBCL003', width: 160, title: '关键评估说明'}
            , {field: 'GWBC002', width: 160, title: '重要等级', hide:true}
        ]],
        cols3 = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWBJSEQ', width: 80, title: '项次'}
            , {field: 'GWBJ001', width: 140, title: '竞争对手'}
            , {field: 'GWBJ002', width: 140, title: '产品描述'}
            , {field: 'GWBJ003', width: 140, title: '优点'}
            , {field: 'GWBJ004', width: 140, title: '缺点', edit: 'text'}
            , {field: 'GWBJ005', width: 140, title: '竞争等级'}
        ]],
        gwatTable1,
        gwatTable2,
        gwatTable3,
        callObj,
        pageVue
    ;

    var Gwat160Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwat160_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                },{
                    dom: 'gwat160_edit_gwbj', tpl: 'gwat160_edit_gwbj'
                }
                ]
            }, win);
        },

        initTable1: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwatTable1 = table.render({
                    elem: app.getByid(doc, 'gwat160_edit_table1'),
                    id: 'gwat160_edit_table1',
                    cols: cols1,
                    data:[],
                    height:'250',
                    page: false
                });

                //监听单元格编辑
                table.on('edit(gwat160_edit_table1)', function (obj) {
                    var tableData = obj.data;
                    if (obj.field == 'GWBK003') {//数量
                        var GWBK003 = obj.value;
                        var GWBK005 = math.mul(tableData.GWBK004,GWBK003).toFixed(3);
                        var GWBK007 = math.mul(GWBK005, tableData.GWBK006).toFixed(3);
                        obj.updateOfEdit({
                            GWBK005:GWBK005,
                            GWBK007:GWBK007
                        })
                    }else if(obj.field == 'GWBK004'){//单价
                        var GWBK004 = obj.value;
                        var GWBK005 = math.mul(tableData.GWBK003, GWBK004).toFixed(3);
                        var GWBK007 = math.mul(GWBK005, tableData.GWBK006).toFixed(3);
                        obj.updateOfEdit({
                            GWBK005:GWBK005,
                            GWBK007:GWBK007
                        })
                    }else if(obj.field == 'GWBK005'){//金额
                        var GWBK005 = obj.value;
                        var GWBK007 = math.mul(GWBK005,tableData.GWBK006).toFixed(3);
                        obj.updateOfEdit({
                            GWBK007:GWBK007
                        })
                    }else if(obj.field == 'GWBK006'){//折扣率
                        var GWBK006 = obj.value;
                        var GWBK007 = math.mul(tableData.GWBK005 ,tableData.GWBK006).toFixed(3);
                        obj.updateOfEdit({
                            GWBK007:GWBK007
                        })
                    }else if(obj.field == 'GWBK007'){

                    }
                });
            });
        },

        initTable2: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwatTable2 = table.render({
                    elem: app.getByid(doc, 'gwat160_edit_table2'),
                    id: 'gwat160_edit_table2',
                    cols: cols2,
                    data:[],
                    height:'250',
                    page: false
                });

            });
        },

        initTable3: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwatTable3 = table.render({
                    elem: app.getByid(doc, 'gwat160_edit_table3'),
                    id: 'gwat160_edit_table3',
                    cols: cols3,
                    data:[],
                    height:'250',
                    page: false
                });

            });
        },

        table1Reload:function (listData) {
            self._reloadTable({id:'gwat160_edit_table1',data: listData});
        },

        table2Reload:function (listData) {
            self._reloadTable({id:'gwat160_edit_table2',data: listData});
        },

        table3Reload:function (listData) {
            self._reloadTable({id:'gwat160_edit_table3',data: listData});
        },

        editData: function (listData1,listData2) {
            app.ajax({
                xurl: 'crm/gwat160/edit',
                xdata: {listData1:listData1,listData2:listData2,data:vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: self._moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwat160').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        addData: function (listData1,listData2) {
            app.ajax({
                xurl: 'crm/gwat160/add',
                xdata: {listData1:listData1,listData2:listData2,data:vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: self._moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwat160').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            app.ajax({
                xurl: 'crm/gwat160/listGwbf',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        vueData.gwbf = result.data.data;
                        if (tagData.is_add) {
                            vueData.isAdd=true;
                            vueData.disabled = false;
                            app.extendUpLow({
                                target: vueData.form,
                                res: {
                                    GWBH034: '1',
                                    GWBH030: new Date().Format("yyyy-MM-dd")
                                },
                                reset: true
                            });
                        } else {
                            vueData.isAdd = false;
                            vueData.disabled = true;
                            callObj = tagData.obj;
                            self.getGwbh011(callObj.data.GWBHNAME, callObj.data.GWBHID, callObj.data.GWBHSEQ);
                            app.extendUpLow({
                                target: vueData.form,
                                res: callObj.data,
                                reset: true
                            });
                        }
                        self.getSpmx();
                        self.getTable2();
                        self.getTable3();
                    }
                }
            });
        },

        getGwbh011:function (GWBHNAME, GWBHID, GWBHSEQ) {
            app.ajax({
                async:false,
                xurl: 'crm/gwat160/getGwbh011',
                xdata: {
                    GWBHNAME: GWBHNAME,
                    GWBHID: GWBHID,
                    GWBHSEQ: GWBHSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        if(result.data.data != null){
                            if (result.data.data.GWBH011.indexOf('{') != -1) {
                                var d = JSON.parse(result.data.data.GWBH011);
                                vueData.gwbf.forEach(function(val) {
                                    var item = d[val.GWBF002];
                                    if (item) {
                                        val.msg = item;
                                    }
                                });
                            }
                        }
                    }
                }
            });
        },
        
        getSpmx: function () {
            app.ajax({
                xurl: 'crm/gwat160/gwat160Spmx',
                xdata: app.cloneJSON(vueData.form),
                cb: function (result) {
                    if (result.ok) {
                        if(result.data.data != null){
                            var table_list = [];
                            for (var i = 0, j = result.data.data.length; i < j; i++) {
                                var item = result.data.data[i];
                                table_list.push(item);
                            }
                            self.table1Reload(table_list);
                        }
                    }
                }
            });
        },

        getTable2: function () {
            app.ajax({
                xurl: 'crm/gwat160/getGwblData',
                xdata: app.cloneJSON(vueData.form),
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        if(result.data.data != null){
                            var table_list = result.data.data;
                            self.table2Reload(table_list);
                        }
                    }
                }
            });
        },

        getTable3: function () {
            app.ajax({
                xurl: 'crm/gwat160/getGwbjData',
                xdata: app.cloneJSON(vueData.form),
                cb: function (result) {
                    if (result.ok) {
                        if(result.data.data != null){
                            var table_list = result.data.data;
                            self.table3Reload(table_list);
                        }else {
                            var table_list = [];
                            self.table3Reload(table_list);
                        }
                    }
                }
            });
        },

        _ready: function () {
            vueData.form = app.fields_(['GWBH_T'],vueData.form);
            pageVue = new Vue({
                el: '#gwat160_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                var _gwbh011 = {};
                                for (var i = 0; i < pageVue.gwbf.length; i++) {
                                    var _msg = pageVue.gwbf[i].msg;
                                    if (_msg) {
                                        _gwbh011[pageVue.gwbf[i].GWBF002] = _msg;
                                    }
                                }
                                pageVue.form.GWBH011 = JSON.stringify(_gwbh011);
                                var tableList = gwatTable1.getDatas();
                                tableList.addMap(vueData.form, false);
                                if(tableList.length == 0){
                                    tableList.push(vueData.form);
                                }
                                var tableList2 = [];
                                var checkStatus = table.checkStatus('gwat160_edit_table2');
                                if (checkStatus.data.length > 0) {
                                    checkStatus.data.forEach(function (v) {
                                        tableList2.push({
                                            GWBL001: v.GWBC001,
                                            GWBL002: v.GWBC002
                                        });
                                    });
                                }
                                if (vueData.isAdd) {
                                    self.addData(tableList,tableList2);
                                } else {
                                    self.editData(tableList,tableList2);
                                }
                            }
                        });
                    },
                    sureGwbl:function () {

                    },
                    addGwbj: function () {
                        if (vueData.form.GWBHID == '' || vueData.form.GWBHID == null) {
                            app.showMessage("新增时客户不能为空");
                            return ;
                        } else if (vueData.form.GWBGL003 == '' || vueData.form.GWBGL003 == null) {
                            app.showMessage('新增时商机主题不能为空');
                            return ;
                        }
                        var data = gwatTable3.getDatas();
                        var count = 0;
                        if(data.length > 0){
                            count = parseInt(data[data.length-1].GWBJSEQ);
                        }
                        self._openTplFull({
                            tag: 'gwat160_edit_gwbj',
                            tagData: {
                                GWBHNAME: vueData.form.GWBHNAME,
                                GWBHID: vueData.form.GWBHID,
                                GWBJSEQ: count+1
                            },
                            index: 1,
                            openOpts: {
                                title: '添加竞争信息',
                                area: ['600px', '430px']
                            }
                        });
                    },
                    addGwbk: function () {
                        if (vueData.form.GWBHID == '' || vueData.form.GWBHID == null) {
                            app.showMessage("新增时客户不能为空");
                            return ;
                        } else if (vueData.form.GWBGL003 == '' || vueData.form.GWBGL003 == null) {
                            app.showMessage('新增时商机主题不能为空');
                            return ;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                multiple: true,
                                config: {
                                    method: 'cq_gwam001_2',
                                    columnList: [{label: '商品编号', prop: 'GWAM001', width: '150'},
                                        {label: '品名', prop: 'GWAML003', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'},
                                        {label: '单价', prop: 'GWAP100', width: '150',unshow:true},
                                        {label: '销售单位', prop: 'GWAM008', width: '150',unshow:true}
                                    ],
                                    dataList: [],
                                    key: 'ROW_ID'
                                },
                                cb: function (data) {
                                    if(data.length > 0){
                                        app.ajax({
                                            xurl: 'crm/gwat160/getCustomerData',
                                            xdata: {GWBHID:vueData.form.GWBHID},
                                            cb: function (result) {
                                                if (result.ok) {
                                                    var huiLv = result.data.data.huiLv || 1; //汇率
                                                    var listData = gwatTable1.getDatas();
                                                    var GWBKSEQ = 0;
                                                    if(listData.length > 0){
                                                        GWBKSEQ = parseInt(listData[listData.length-1].GWBKSEQ);
                                                    }
                                                    for(var i = 0; i < data.length; i++){
                                                        listData.push({
                                                            GWBKSEQ: GWBKSEQ + i,
                                                            GWBK001:data[i].GWAM001,
                                                            GWAML003: data[i].GWAML003,
                                                            GWAML004: data[i].GWAML004,
                                                            GWBK003: 1,
                                                            GWBK004: data[i].GWAP100 || 0,
                                                            GWBK005: data[i].GWAP100 || 0,
                                                            GWBK006: huiLv,
                                                            GWBK007: math.mul((data[i].GWAP100 || 0),huiLv),
                                                            GWBK008: '',
                                                            GWBK009: '',
                                                            GWBK002: data[i].GWAM008
                                                        });
                                                    }
                                                    self.table1Reload(listData);
                                                }
                                            }
                                        });
                                    }
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择商品信息',
                                area: ['900px', '500px']
                            }
                        });
                    },
                    del: function () {
                        var list1 = gwatTable1.getDatas();
                        var checkStatus = table.checkStatus('gwat160_edit_table1');
                        if (checkStatus.data.length > 0) {
                            checkStatus.data.forEach(function (v) {
                                list1.removeByMap({
                                    GWBKSEQ: v.GWBKSEQ
                                });
                            });
                            self.table1Reload(list1);
                        }
                    },
                    delGwbj: function () {
                        var flag = true;
                        var checkStatus = table.checkStatus('gwat160_edit_table3');
                        if (checkStatus.data.length > 0) {
                            var _xdata = [];
                            checkStatus.data.forEach(function (v) {
                                var _temp = app.fields_('GWBJ_T');
                                _temp.GWBJNAME = vueData.form.GWBHNAME;
                                _temp.GWBJID = vueData.form.GWBHID;
                                _temp.GWBJSEQ = v.GWBJSEQ;
                                _xdata.push(_temp);
                            });
                        }else {
                            app.showWarning("请先选择数据！");
                            return;
                        }
                        if (flag) {
                            app.ajax({
                                xurl: 'crm/gwat160/delGwbjData',
                                xdata: _xdata,
                                loading: true,
                                toJsonStr: true,
                                confirm: {msg: "是否删除选择的数据？"},
                                okmsg: '删除成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        self.getTable3();
                                    }
                                }
                            });
                        }

                    },
                    openGwakl004:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'cq_gwak001_1',
                                    columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ],
                                    searchParam:{
                                        GWAK006: '1'
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWBHID = data.GWAK001;
                                    vueData.form.GWAKL004 = data.GWAKL004;

                                    vueData.form.GWBH016 = '';
                                    vueData.form.GWAKL003 = '';

                                    vueData.form.GWBHNAME = '';
                                    vueData.form.GWBGL003 = '';
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择拜访客户',
                                area: ['800px', '470px']
                            }
                        });
                    },
                    openGwbgl003:function () {
                        if (vueData.form.GWAKL004 == '' || vueData.form.GWAKL004 == null) {
                            app.showMessage("请先选择客户！");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwbg001',
                                    columnList: [{label: '商机编号', prop: 'GWBG001', width: '150'},
                                        {label: '说明', prop: 'GWBGL003', width: '150'},
                                        {label: '助记码', prop: 'GWBGL004', width: '150'}
                                    ],
                                    searchParam:{
                                        GWBG002: vueData.form.GWBHID
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWBHNAME = data.GWBG001;
                                    vueData.form.GWBGL003 = data.GWBGL003;
                                    self.getSpmx();
                                    self.getTable3();
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择商机主题',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwakl003:function () {
                        if (vueData.form.GWBHNAME == '' || vueData.form.GWBHNAME == null) {
                            app.showMessage("请先选择商机主题！");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'cq_gwak001_1',
                                    columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ],
                                    searchParam:{
                                        GWAK006: '2',
                                        GWBE001: vueData.form.GWBHNAME
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWBH016 = data.GWAK001;
                                    vueData.form.GWAKL003 = data.GWAKL004;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择负责经销商',
                                area: ['800px', '470px']
                            }
                        });
                    },
                    openGwavl003:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwav001',
                                    columnList: [{label: '商机阶段编号', prop: 'GWAV001', width: '150'},
                                        {label: '商机阶段说明', prop: 'GWAVL003', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWBH028 = data.GWAV001;
                                    vueData.form.GWAVL003 = data.GWAVL003;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择商机阶段',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwbh006:function () {
                        if (vueData.form.GWAKL004 == '' || vueData.form.GWAKL004 == null) {
                            app.showMessage("请先选择客户！");
                            return;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwal021',
                                    columnList: [
                                        {label: '联系人', prop: 'GWAL021', width: '150'},
                                        {label: '电话', prop: 'GWAL020', width: '150'}
                                    ],
                                    searchParam:{
                                        GWAL001: vueData.form.GWBHID
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWBH006 = data.GWAL021;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择拜访人员',
                                area: ['800px', '470px']
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwat160_edit');
                }
            });

            self.initTable1();
            self.initTable2();
            self.initTable3();
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwat160Edit();

});
